<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<button id="createUserAgent">注册上线</button>
<button id="destroyUserAgent">销毁下线</button>
<button id="pickup">接听</button>
<button id="hangup">挂断</button>
<br/>
<input type="text" id="number"/><button id="call">呼叫</button>
<div id="msg"></div>
</body>
<script>
    const pickupButton = document.getElementById("pickup")
    const hangupButton = document.getElementById("hangup")
    const callButton = document.getElementById("call")
    const createUserAgentButton = document.getElementById("createUserAgent")
    const destroyUserAgentButton = document.getElementById("destroyUserAgent")
    const number = document.getElementById("number")
    const msgBox = document.getElementById("msg")
    let webSocket = new WebSocket('ws://127.0.0.1:8123/sip');
    let registerData = {
        "code": "createUserAgent",
        "sipUserInfo": {
            "number": "103",
            "pwd": "103",
            "server": "192.168.110.129:6060"
        }
    }
        webSocket.onopen = () => {
            webSocket.send(JSON.stringify(registerData))
        }
        webSocket.onmessage = ({data}) => {
            let json = JSON.parse(data)
            console.log(json)
            // 创建新元素
            const newElement = document.createElement('p');
            newElement.textContent = `time：${getTime()} msg：${json.msg} code：${json.code} `;

            // 在父元素的开头插入新元素
            msgBox.prepend(newElement);
        }
    callButton.onclick = ()=>{
        if(number.value){
            let data = {"code": "call","number":number.value}
            webSocket.send(JSON.stringify(data))
        }else {
            alert("无号码")
        }
    }
    pickupButton.onclick = ()=>{
        let data = {"code": "pickup",}
        webSocket.send(JSON.stringify(data))
    }
    hangupButton.onclick = ()=>{
        let data = {"code": "hangup",}
        webSocket.send(JSON.stringify(data))
    }
    destroyUserAgentButton.onclick = ()=>{
        let data = {"code": "destroyUserAgent",}
        webSocket.send(JSON.stringify(data))
    }
    createUserAgentButton.onclick = ()=>{
        webSocket.send(JSON.stringify(registerData))
    }

    function getTime(){
        // 创建一个 Date 对象，表示当前时间
        const currentDate = new Date();

// 获取年、月、日、时、分、秒
        const year = currentDate.getFullYear();
// 月份从 0 开始，所以要加 1
        const month = String(currentDate.getMonth() + 1).padStart(2, '0');
        const day = String(currentDate.getDate()).padStart(2, '0');
        const hours = String(currentDate.getHours()).padStart(2, '0');
        const minutes = String(currentDate.getMinutes()).padStart(2, '0');
        const seconds = String(currentDate.getSeconds()).padStart(2, '0');

// 拼接成所需的格式，例如 YYYY-MM-DD HH:MM:SS
        const formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
        console.log(formattedDate);
        return formattedDate;
    }
</script>
</html>